var numi = 1;
let pages = 0;
let page = 0;
var fenyearr = "";
var classs = JSON.parse(localStorage.getItem("classs"));
var employees = JSON.parse(localStorage.getItem("employees"));
var classtype = JSON.parse(localStorage.getItem("classtype"));
var Department = JSON.parse(localStorage.getItem("Department"));
var Department = JSON.parse(localStorage.getItem("Department"));
var student = JSON.parse(localStorage.getItem("student"));
$(document).ready(function(){
    initialize(student);
})
$(".sear").click(function (e) { 
    e.preventDefault();
    let str = $(".sear_tiao").val();
    page = 0;
    searDep(str);
});
$(".bu").change(function (e) { 
    e.preventDefault();
    let str = $(".bu").val();
    page = 0;
    searDep1(str);
});
$("#addq").click(function (e) { 
    e.preventDefault();
    let name = $(".stna").val();
    let classname = $(".bu1").val();
    let age = $(".stnaage").val();
    let sex = $(".sex").val();
    let jzname = $(".jzname").val();
    let tel = $(".jztel").val();
    let addr = $(".jzaddr").val();
    var result = classs.findIndex((v) => {
        return v.classname === classname;
    });


    if(name == "" ||classname == "") {

    }else {
        addDep(name,result,jzname,tel,age,sex,addr,student);
    }
    $("#depAdd").modal('hide');
    $(".stna").val("");
    $(".bu1").val("");
    $(".stnaage").val("");
    $(".sex").val("");
    $(".jzname").val("");
    $(".jztel").val("");
    $(".jzaddr").val("");
})
$("#delq").click(function (e) { 
    e.preventDefault();
    delDep(numi);
});
$("#upq").click(function (e) { 
    e.preventDefault();
    if($(".upname").val() !="") {
        student[numi].Name = $(".upname").val();
        let classname = $(".up").val();
        var result = classs.findIndex((v) => {
            return v.classname === classname;
        });
        student[numi].Grade = classs[result].classtype;
        student[numi].Gradeid = classs[result].typeid;
        student[numi].ClassTypeName = classs[result].classname;
        student[numi].classid = classs[result].id;
        student[numi].Age = $(".upage").val();
        let sex = $(".sex").val();
        if(sex ==0) {
            sex = "男";
        }else {
            sex = "女";
        }
        student[numi].Sex = sex;
        student[numi].LinkName1 = $(".upjzname").val();
        student[numi].Tel1 = $(".upjztel").val();
        student[numi].Address = $(".upjzaddr").val();
        $("#depUp").modal('hide');
        $(".upage").val("");
        $(".sex").val("");
        $(".upjzname").val("");
        $(".upjztel").val("");
        $(".upjzaddr").val("");
        localStorage.setItem("student", JSON.stringify(student));
        initia(student);
    }else {
        $("#depUp").modal('hide');
        initia(student);
    }
    
});
$(".juzhong a").click(function (e) { 
    e.preventDefault();
    num = $(this).parent().val();
    if(page != 1) {
        if(num==98) {
            page--;
            initia(fenyearr);
        }
    }
    if(page != pages) {
        if(num==99) {
            page++
            initia(fenyearr);
        }
    }
});

$('#depDel').on('show.bs.modal', function (event) {
    let numid = $($(event.relatedTarget).parent().parent().siblings()).html();
    console.log(numid);
    var result = student.findIndex((v) => {
        return v.num === numid;
    });
    numi = result;
    $(".stunum").html(student[result].num);
    $(".stuname").html(student[result].Name);
})
$('#depUp').on('show.bs.modal', function (event) {
    let numid = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = student.findIndex((v) => {
        return v.num === numid;
    });
    numi = result;
    $(".up").empty();
    $(".upsex option").removeAttr("selected");
    for (let i = 0; i < classs.length; i++) {
        let str = "";
        if(student[result].classid == classs[i].id) {
            str = `<option selected >${classs[i].classname}</option>`;
        }else {
            str = `<option>${classs[i].classname}</option>`; 
        }
        $(".up").append(str);
    }
    if(student[result].Sex == $(".upsex option:first-child").html()) {
        $(".upsex option:first-child").attr("selected","selected");
    }else {
        $(".upsex option:last-child").attr("selected","selected");
    }
    // console.log(result);
    $(".upname").attr("placeholder",student[result].Name);
    $(".upage").attr("placeholder",student[result].Age);
    $(".upjzname").attr("placeholder",student[result].LinkName1);
    $(".upjztel").attr("placeholder",student[result].Tel1);
    $(".upjzaddr").attr("placeholder",student[result].Address);
})
$('#depShow').on('show.bs.modal', function (event) {
    let numid = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = student.findIndex((v) => {
        return v.num === numid;
    });
    $(".showname").html(student[result].Name);
    $(".showage").html(student[result].Age);
    $(".showsex").html(student[result].Sex);
    $(".showjzname").html(student[result].LinkName1);
    $(".showjztel").html(student[result].Tel1);
    $(".showaddr").html(student[result].Address);
})



//初始化加载
function initialize(arr1) {
    fenyearr = arr1;
    for (let i = 0; i < classs.length; i++) {
        let str = `<option>${classs[i].classname}</option>`;
        $(".bu").append(str);
        $(".bu1").append(str);
    }
    let arr = fenye(arr1);
    let teache1 =  "";
    for(let i = 0; i<arr.length; i++) {
        for (let j = 0; j < classs.length; j++) {
            if (arr[i].classid == classs[j].id) {
                teache1 = classs[j].teacher1;
            }
        }
        let str1 = `<tr>
                        <td>${arr[i].num}</td>
                        <td>${arr[i].Name}</td>
                        <td>${arr[i].ClassTypeName}</td>
                        <td>${teache1}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        $(".dep_table").append(str1);
    }
}
//二次加载
function initia(arr1) {
    fenyearr = arr1;
    $(".biaotou").siblings().remove();
    let arr = fenye(arr1).concat();
    let teache1 =  "";
    for(let i = 0; i<arr.length; i++) {
        for (let j = 0; j < classs.length; j++) {
            if (arr[i].classid == classs[j].id) {
                teache1 = classs[j].teacher1;
            }
        }
        let str1 = `<tr>
                        <td>${arr[i].num}</td>
                        <td>${arr[i].Name}</td>
                        <td>${arr[i].ClassTypeName}</td>
                        <td>${teache1}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        $(".dep_table").append(str1);
    }
}
//新增re
function addDep(name,re,jzname,tel,age,sex,addr,arr){
    let timedate = new Date;
    let str = `${timedate.getFullYear()}-${timedate.getMonth()+1}-${timedate.getDate()}`;
    let num = parseInt(arr[arr.length-1].num)+1;
    if(num % 10 != 0) {
        num = "00"+ num;
    }else if(num % 100 != 0) {
        num += 1;
    }else {
        num = "00" + num;
    }
    if(sex ==0) {
        sex = "男";
    }else {
        sex = "女";
    }
    var add = {
        id : arr.length+1,
        num : `${num}`,
        Grade: `${classs[re].classtype}`,
        Gradeid : classs[re].typeid,
        Name: `${name}`,
        LinkName1: jzname,
        Relation1: "",
        Tel1: tel,
        LinkName2: "",
        Relation2: "",
        Tel2: "",
        Age: age,
        Sex: sex,
        Address: addr,
        ClassTypeName: `${classs[re].classname}`,
        classid : `${classs[re].id}`
    }
    arr.push(add);
    localStorage.setItem("student",JSON.stringify(arr));
    student = JSON.parse(localStorage.getItem("student"));
    initia(student);
}
//删除
function delDep(numi) {
    student.splice(numi,1);
    localStorage.setItem("student", JSON.stringify(student));
    student = JSON.parse(localStorage.getItem("student"));
    $("#depDel").modal('hide');
    initia(student);
}
//修改
function upDep() {

}
//查看
function showDep() {

}
//搜索
function searDep(str) {
    let arr1 = [];
    student = JSON.parse(localStorage.getItem("student"));
    if(str != "") {
        var result = student.findIndex((v) => {
            return v.Name === str;
        });
        if(result == -1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        } else {
            arr1.push(student[result]);
            initia(arr1);
        }
    }else {
        if(employees.length == $(".dep_table tbody").children().length-1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        }else {
            location.reload();
        }
    }
}
function searDep1(str) {
    let arr1 = [];
    student = JSON.parse(localStorage.getItem("student"));
    if(str == "全部") {
        location.reload();
    }else {
        var result = classs.findIndex((v) => {
            return v.classname === str;
        });
        for(let i = 0; i<student.length; i++) {
            if(student[i].classid == classs[result].id){
                arr1.push(student[i]);
            }
        }
        // console.log(arr1);
        initia(arr1);
    }
}
function fenye (arr) {
    let xia  = $(".xia").clone(true);
    $(".shang").nextAll().remove();
    $(".shang").after(xia);
    let num = 0;
    if(arr.length<=6) {
        pages = 1
        num = arr.length;
    }else {
        pages = parseInt(arr.length/6);
        num = 6;
        if(arr.length%6 != 0) {
            pages += 1;
            
        }
    }
    for (let i = 1; i <= pages; i++) {
        $(".xia").before(`<li class= ab,a${i} value = ${i}><a onclick = "dijifenye(this)">${i}</a></li>`);
    }
    if(page == 0) {
        $(".a1").addClass("active");
        page=1;
    }else {
        $(`.a${page}`).addClass("active");
        
    }
    if(page == 1) {
        $(".shang").addClass("disabled");
    }else {
        $(".shang").removeClass("disabled")
    }
    if(page == pages) {
        $(".xia").addClass("disabled");
    }else {
        $(".xia").removeClass("disabled")
    }
    let arr1 = arr.concat();

    return arr1.splice(page*6-6,num);
}
function dijifenye(num) {
    page = parseInt($(num).html());
    initia(fenyearr);
}







